package com.hehao.reactorPractice;

import com.hehao.entity.User;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.List;

/**
 * @Author Damon
 * @Date 2024/5/8 16:30
 */
public class OperationPractice {
    // TODO Create a Flux of user from Flux of username, firstname and lastname.从用户名、名字和姓氏的 Flux 中创建一个用户的 Flux。
    Flux<User> userFluxFromStringFlux(Flux<String> usernameFlux, Flux<String> firstnameFlux, Flux<String> lastnameFlux) {
        return Flux.zip(usernameFlux, firstnameFlux, lastnameFlux)
                .map(tuple -> new User(tuple.getT1(), tuple.getT2(), tuple.getT3()));
    }

//========================================================================================

    // TODO Return the mono which returns its value faster 返回最快返回值的 Mono
    Mono<User> useFastestMono(Mono<User> mono1, Mono<User> mono2) {
        return Mono.firstWithValue(mono1, mono2);
    }

//========================================================================================

    // TODO Return the flux which returns the first value faster返回最快返回第一个值的 Flux。
    Flux<User> useFastestFlux(Flux<User> flux1, Flux<User> flux2) {
        return Flux.firstWithValue(flux1, flux2);
    }

//========================================================================================

    // TODO Convert the input Flux<User> to a Mono<Void> that represents the complete signal of the flux
//    输入的 Flux<User> 转换为表示 Flux 完成信号的 Mono<Void>
    Mono<Void> fluxCompletion(Flux<User> flux) {
        return flux.then();
    }

//========================================================================================

    // TODO Return a valid Mono of user for null input and non null input user (hint: Reactive Streams do not accept null values)
//    对于空输入和非空输入用户，返回一个有效的用户 Mono（提示：Reactive Streams 不接受空值）。
    Mono<User> nullAwareUserToMono(User user) {
        return Mono.justOrEmpty(user);
    }

//========================================================================================

    // TODO Return the same mono passed as input parameter, expect that it will emit User.SKYLER when empty
//    返回与输入参数相同的 Mono，但在为空时发出 User.SKYLER。
    Mono<User> emptyToSkyler(Mono<User> mono) {
        return mono.switchIfEmpty(Mono.just(User.SKYLER));
    }

//========================================================================================

    // TODO Convert the input Flux<User> to a Mono<List<User>> containing list of collected flux values
//    将输入的 Flux<User> 转换为包含收集的 Flux 值列表的 Mono<List<User>>。
    Mono<List<User>> fluxCollection(Flux<User> flux) {
        return flux.collectList();
    }
}
